package com.xc.ksxt.service;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import com.xc.common.tool.Helper;
import com.xc.db.tables.records.LibCategoryRecord;
import com.xc.ksxt.entity.LibQuestionEntity;
import com.xc.ksxt.vo.LibQuestionVO;
import com.xc.common.object.PageInfo;
import com.xc.db.Tables;
import com.xc.db.tables.LibQuestion;
import com.xc.db.tables.records.LibQuestionRecord;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.InsertResultStep;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SelectForUpdateStep;
import org.jooq.SelectSeekStepN;
import org.jooq.SortField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LibQuestionService {
	@Autowired
	private DSLContext dsl;
	private LibQuestion LIBQUESTIONTB = LibQuestion.LIB_QUESTION.as("LIBQUESTIONTB");
	public LibQuestion getTable() {
		return LIBQUESTIONTB;
	}
	public Result<Record> select(Condition whr, PageInfo<LibQuestionEntity> pageInfo, SortField<?>... orderby) {
		SelectSeekStepN<Record> seek = dsl.select().from(LIBQUESTIONTB).where(whr).orderBy(orderby);
		Result<Record> result = null;
		if (pageInfo != null) {
			SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
			result = su.fetch();
		} else {
			result = seek.fetch();
		}
		if (pageInfo != null) {
			pageInfo.setResult(result);
			pageInfo.setList(result.into(LibQuestionEntity.class));
		}
		return result;
	}
	public List<LibQuestionVO> selectVO(Condition whr, SortField<?>... orderby) {
		SelectSeekStepN<Record> seek = dsl.select().from(LIBQUESTIONTB).where(whr).orderBy(orderby);
		Result<Record> result = seek.fetch();
		if(result.isEmpty()){
			return null;
		}
		return result.into(LibQuestionVO.class);
	}
	public Result<Record> selectVO(Condition whr, PageInfo<LibQuestionVO> pageInfo, SortField<?>... orderby) {
		SelectSeekStepN<Record> seek = dsl.select().from(LIBQUESTIONTB).where(whr).orderBy(orderby);
		Result<Record> result = null;
		if (pageInfo != null) {
			SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
			result = su.fetch();
		} else {
			result = seek.fetch();
		}
		if (pageInfo != null) {
			pageInfo.setResult(result);
			pageInfo.setList(result.into(LibQuestionVO.class));
		}
		return result;
	}

	public List<LibQuestionEntity> select(Condition whr, SortField<?>... orderby) {
		Result<Record> result = dsl.select().from(LIBQUESTIONTB).where(whr).orderBy(orderby).fetch();
		List<LibQuestionEntity> list = Lists.newArrayList();
		if (result.size() > 0) {
			list = result.into(LibQuestionEntity.class);
		}
		return list;
	}

	public Long count(Condition cond) {
		Long count = dsl.selectCount().from(LIBQUESTIONTB).where(cond).fetchOneInto(Long.class);
		return count;
	}

	public Record getRecordById(Long id) {
		Record record = dsl.select().from(LIBQUESTIONTB).where(LIBQUESTIONTB.ID.eq(id)).fetchOne();
		return record;
	}
	public Record getRecordById(String id) {
		Record record = getRecordById(Long.valueOf(id));
		return record;
	}

	public LibQuestionEntity getEntityById(Long id) {
		Record r = getRecordById(id);
		if(r==null) {
			return null;
		}
		LibQuestionEntity entity = r.into(LibQuestionEntity.class);
		return entity;
	}

	public String update(LibQuestionEntity entity) {
		LibQuestionRecord record = new LibQuestionRecord();
		record.from(entity);
		dsl.executeUpdate(record);
		return entity.getId();
	}

	public String insert(LibQuestionEntity entity) {
		LibQuestionRecord record = new LibQuestionRecord();
		if(StringUtils.isEmpty(entity.getId())) {
			String id = Helper.getSnowId() + "";
			entity.setId(id);
		}

		record.from(entity);
		dsl.executeInsert(record);
		return entity.getId();
	}

	public String save(LibQuestionEntity entity) {
		if (entity.getId()==null||entity.getId().isEmpty()) {
			return insert(entity);
		}
		Record r = getRecordById(entity.getId());
		if(r==null){
			return insert(entity);
		}
		return update(entity);
	}

	public void deleteById(Long id) {
		dsl.delete(LIBQUESTIONTB).where(LIBQUESTIONTB.ID.eq(id)).execute();
	}
	public static Cache<String, List<LibQuestionVO>> JOINCACHE = CacheBuilder.newBuilder()
			.expireAfterWrite(2, TimeUnit.DAYS)
			.build();
	public static Cache<String, List<LibQuestionVO>> JOINCACHE2 = CacheBuilder.newBuilder()
			.expireAfterWrite(2, TimeUnit.DAYS)
			.build();
}
